<?php
class RecommendWidget extends BaseWidget{
	/**
	 * 显示前获取需要显示的数据
	 */
	public function get_data(){
		$mod = new RecommendModel();
		
		$type = $this->config['type'];
		$main_count = $this->config['main'];
		$recommend = $mod->get_by_attrs(array('site_id'=>SITE_ID, 'type'=>$type));
		$recommend['items'] = SortHelper::sortByCol($recommend['items'], 'rank');
		
		$i = 0;
		$cate_ids = array();
		foreach($recommend['items'] as $id=>$recommend_item){
			$recommend['items'][$id] = $this->_init_article($id, $recommend_item['title']);
		}
// 		print_r($recommend['items']);exit;
		return array(
			'articles' => $recommend['items']
		);
	}
	
	private function _init_article($article_id, $title){
		$article_mod = new ArticleModel();
		$article = $article_mod->get_by_key_site_limit($article_id, 'title,cate,alias');
		$article['title'] = $title;
		$cate_id = $article['cate'];
		if(is_array($article['cate'])){
			$cate_id = $article['cate'][0];
		}
		$cate = null;
		if(!empty($cate_id)){
			$cate = Article_cateModel::get_cate_by_key($cate_id, 'path,name');
		}
		if($cate!==null){
			$article['cate_name'] = $cate['name'];
			$article['cate_path'] = $cate['path'];
			init_article_url($article, $cate['path']);
		}else{
			$article['url'] = '/' . $article['alias']. '.html';
		}
		return $article;
	}
}

?>